<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Advanced Usage of Zend_Json - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.json.advanced.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.json.advanced.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.json.basics.html">Basic Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.json.html">Zend_Json</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.json.xml2json.html">XML to JSON conversion</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.json.advanced" class="section"><div class="info"><h1 class="title">Advanced Usage of Zend_Json</h1></div>
    

    <div class="section" id="zend.json.advanced.objects1"><div class="info"><h1 class="title">JSON Objects</h1></div>
        

        <p class="para">
            When encoding <acronym class="acronym">PHP</acronym> objects as <acronym class="acronym">JSON</acronym>, all public
            properties of that object will be encoded in a <acronym class="acronym">JSON</acronym> object.
        </p>

        <p class="para">
            <acronym class="acronym">JSON</acronym> does not allow object references, so care should be taken not to
            encode objects with recursive references. If you have issues with
            recursion,  <span class="methodname">Zend_Json::encode()</span> and
             <span class="methodname">Zend_Json_Encoder::encode()</span> allow an optional second
            parameter to check for recursion; if an object is serialized twice, an
            exception will be thrown.
        </p>

        <p class="para">
            Decoding <acronym class="acronym">JSON</acronym> objects poses an additional difficulty, however, since
            Javascript objects correspond most closely to <acronym class="acronym">PHP</acronym>&#039;s associative
            array. Some suggest that a class identifier should be passed, and an object
            instance of that class should be created and populated with the
            key/value pairs of the <acronym class="acronym">JSON</acronym> object; others feel this could pose a
            substantial security risk.
        </p>

        <p class="para">
            By default, <span class="classname">Zend_Json</span> will decode <acronym class="acronym">JSON</acronym> objects
            as associative arrays. However, if you desire an object returned, you can specify this:
        </p>

        <pre class="programlisting brush: php">
// Decode JSON objects as PHP objects
$phpNative = Zend_Json::decode($encodedValue, Zend_Json::TYPE_OBJECT);
</pre>


        <p class="para">
            Any objects thus decoded are returned as <span class="classname">StdClass</span> objects
            with properties corresponding to the key/value pairs in the <acronym class="acronym">JSON</acronym>
            notation.
        </p>

        <p class="para">
            The recommendation of Zend Framework is that the individual
            developer should decide how to decode <acronym class="acronym">JSON</acronym> objects. If an object of a
            specified type should be created, it can be created in the developer
            code and populated with the values decoded using <span class="classname">Zend_Json</span>.
        </p>
    </div>

    <div class="section" id="zend.json.advanced.objects2"><div class="info"><h1 class="title">Encoding PHP objects</h1></div>
        

        <p class="para">
            If you are encoding <acronym class="acronym">PHP</acronym> objects by default the encoding mechanism
            can only access public properties of these objects. When a method
             <span class="methodname">toJson()</span> is implemented on an object to encode,
            <span class="classname">Zend_Json</span> calls this method and expects the object to return a
            <acronym class="acronym">JSON</acronym> representation of its internal state.
        </p>
    </div>

    <div class="section" id="zend.json.advanced.internal"><div class="info"><h1 class="title">Internal Encoder/Decoder</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Json</span> has two different modes depending if ext/json is
            enabled in your <acronym class="acronym">PHP</acronym> installation or not. If ext/json is installed by
            default  <span class="methodname">json_encode()</span> and
             <span class="methodname">json_decode()</span> functions are used for encoding and decoding
            <acronym class="acronym">JSON</acronym>. If ext/json is not installed a Zend Framework implementation in
            <acronym class="acronym">PHP</acronym> code is used for en-/decoding. This is considerably slower than
            using the <acronym class="acronym">PHP</acronym> extension, but behaves exactly the same.
        </p>

        <p class="para">
            Still sometimes you might want to use the internal encoder/decoder even
            if you have ext/json installed. You can achieve this by calling:
        </p>

        <pre class="programlisting brush: php">
Zend_Json::$useBuiltinEncoderDecoder = true:
</pre>

    </div>

    <div class="section" id="zend.json.advanced.expr"><div class="info"><h1 class="title">JSON Expressions</h1></div>
        

        <p class="para">
            Javascript makes heavy use of anonymnous function callbacks, which can be saved
            within <acronym class="acronym">JSON</acronym> object variables. Still they only work if not
            returned inside double qoutes, which <span class="classname">Zend_Json</span> naturally does.
            With the Expression support for <span class="classname">Zend_Json</span> support you can encode
            <acronym class="acronym">JSON</acronym> objects with valid javascript callbacks. This works for both
             <span class="methodname">json_encode()</span> or the internal encoder.
        </p>

        <p class="para">
            A javascript callback is represented using the <span class="classname">Zend_Json_Expr</span>
            object. It implements the value object pattern and is immutable. You can set the
            javascript expression as the first constructor argument. By default
            <span class="classname">Zend_Json::encode</span> does not encode javascript callbacks, you have
            to pass the option <span class="property">enableJsonExprFinder</span> and set it to
            <b><tt>TRUE</tt></b> into the  <span class="methodname">encode()</span> function. If
            enabled the expression support works for all nested expressions in large object
            structures. A usage example would look like:
        </p>

        <pre class="programlisting brush: php">
$data = array(
    &#039;onClick&#039; =&gt; new Zend_Json_Expr(&#039;function() {&#039;
              . &#039;alert(&quot;I am a valid javascript callback &#039;
              . &#039;created by Zend_Json&quot;); }&#039;),
    &#039;other&#039; =&gt; &#039;no expression&#039;,
);
$jsonObjectWithExpression = Zend_Json::encode(
    $data,
    false,
    array(&#039;enableJsonExprFinder&#039; =&gt; true)
);
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.json.basics.html">Basic Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.json.html">Zend_Json</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.json.xml2json.html">XML to JSON conversion</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.json.html">Zend_Json</a></li>
  <li><a href="zend.json.introduction.html">Introduction</a></li>
  <li><a href="zend.json.basics.html">Basic Usage</a></li>
  <li class="active"><a href="zend.json.advanced.html">Advanced Usage of Zend_Json</a></li>
  <li><a href="zend.json.xml2json.html">XML to JSON conversion</a></li>
  <li><a href="zend.json.server.html">Zend_Json_Server - JSON-RPC server</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>